package com.example.recuitactivity.enterprise_fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.R;
import com.example.recuitactivity.adapter.EnterpriseGotDeliversAdapter;
import com.example.recuitactivity.bean.Deliver;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseGotDeliversFragment extends Fragment {
    private View messageLayout;
    private RecyclerView rv;
    private List<Deliver> mDatas=new ArrayList<>();
    private EnterpriseGotDeliversAdapter adapter;
    private DbOpenHelper dbOpenHelper;
    private String username;

    public EnterpriseGotDeliversFragment(String username){
        this.username=username;
    }

    public void initUI() {
        dbOpenHelper=new DbOpenHelper(getActivity());
        rv=(RecyclerView) messageLayout.findViewById(R.id.rv_enterprise_got_delivers);
    }

    public void initRecyclerView(){
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        rv.setLayoutManager(layoutManager);
        adapter=new EnterpriseGotDeliversAdapter(getActivity(),mDatas);
        rv.setAdapter(adapter);
    }

    public void getDatas(){
        String company=dbOpenHelper.getEnterpriseCompanyByUsername(username);
        long eid=dbOpenHelper.getEnterpriseIdByCompany(company);
        mDatas=dbOpenHelper.getDeliversByEId(eid);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.fragment_enterprise_got_delivers, container, false);
        initUI();
        getDatas();
        initRecyclerView();
        return messageLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDatas();
        adapter.setmDatas(mDatas);
    }
}